package main;

/**
 * 
 * @author Dylan
 * 
 * Wait for and respond to any user input. Currently only takes keyboard input.
 * 
 */

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JTextField;

public class Controller extends Thread {
	public Model model;
	private static boolean[] keysPressed;	//Contains boolean values - whether or not keys are pressed
	private static JTextField keyInput;

	public Controller() {
		keysPressed = new boolean[255];
	}

	public void run() {
		while (true) {
			Init.sleep(10);

		}
	}
	
	public boolean pressed(int key){
		return keysPressed[key];
	}

	/**
	 * Add a controller to the specified window and start all relevant threads.
	 * 
	 * @param window
	 * @return
	 */
	public static Controller createController(JFrame window) {
		Controller controller = new Controller();

		keyInput = new JTextField();
		// Set values in keysPressed to true or false depending on whether they
		// are pressed down.
		keyInput.addKeyListener(new KeyListener() {
			public void keyPressed(KeyEvent evt) {
				keysPressed[evt.getKeyCode()] = true;
				//System.out.println(evt.getKeyCode());
				// Quit is esc is pressed
				if (evt.getKeyCode() == 81 && keysPressed[17]) {
					System.exit(0);
				}
			}

			public void keyReleased(KeyEvent evt) {
				keysPressed[evt.getKeyCode()] = false;
			}

			public void keyTyped(KeyEvent evt) {
			}

		});
		window.add(keyInput);
		keyInput.setBounds(0, 0, 0, 0);
		controller.start();
		return controller;
	}
}
